package com.xcity.game.activity.login;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.xcity.db.entity.Player;
import com.xcity.game.activity.ActivityService;
import com.xcity.game.activity.redpacket.RedPacketActivityService;
import com.xcity.game.common.Cause;
import com.xcity.game.common.MessageConst;
import com.xcity.game.common.PlayerPool;
import com.xcity.game.event.GameEvents;
import com.xcity.pomelo.PomeloRoute;
import com.xcity.pomelo.request.DailyLoginRewardRequest;
import com.xcity.pomelo.response.DailyLoginRewardResponse;
import com.xcity.pomelo.response.HintResponse;

import naga.x.App;
import naga.x.game.template.TemplateService;
import naga.x.game.time.TimeUpdater;
import naga.x.net.Session;
import naga.x.net.annotation.OPHandler;
import naga.x.net.impl.pomelo.annotation.PomeloOP;
import naga.x.service.ServiceException;
import naga.x.util.CommonUtils;

/**
 * 每日登录奖励
 * @author YangLi
 *
 */
@OPHandler
public class DailyLoginActivityService extends ActivityService {
	
	private List<DailyLoginRewardTemplate> templates;
	
	private static final Random RND = new Random();
	private static final int SIZE = 9; // 取9个物品，随机奖励其中1个

	@Override
	public void startup() throws ServiceException {
		templates = App.getApp().getServiceManager().get(TemplateService.class).getTemplatesList(DailyLoginRewardTemplate.class);
	}

	@Override
	public void shutdown() throws ServiceException {
	}

	@PomeloOP(PomeloRoute.GET_DAILY_LOGIN_REWARD)
	protected void requestReward(Session session, DailyLoginRewardRequest req) {
		Player player = session.getClient();
		if (!isFirstLoginToday(player)) {
			session.send(new HintResponse(req.getSerial(), MessageConst.REWARD_ALREADY_RECEIVED));
			return;
		}
		player.poolSet(PlayerPool.PROPERTY_LAST_GET_DAILY_REWARD_DAY, TimeUpdater.getInstance().today());
		List<DailyLoginRewardTemplate> list = randomRewards();
		int pos = req.getPos();
		if (pos < 0 || pos >= list.size()) {
			pos = RND.nextInt(list.size());
		}
		DailyLoginRewardTemplate reward = list.get(pos);
		player.getBag().addItemWithoutException(reward.getId(), reward.getCount(), Cause.DAILY_LOGIN);
		session.send(new DailyLoginRewardResponse(req.getSerial(), pos, list));
		if (App.getApp().getServiceManager().get(RedPacketActivityService.class).isEnable()) {
			App.getApp().getEventManager().addEvent(GameEvents.EVENT_DAILY_REWARD_GOT, player);
		}
		LOG.info("[DAILY LOGIN REWARD]ID[{}]ITEM[{}]COUNT[{}]", player.getId(), reward.getId(), reward.getCount());
	}
	
	private List<DailyLoginRewardTemplate> randomRewards() {
		List<DailyLoginRewardTemplate> dup = new ArrayList<DailyLoginRewardTemplate>(templates);
		CommonUtils.shuffle(dup);
		return SIZE < dup.size() ? dup.subList(0, SIZE) : dup;
	}
	
	public static boolean isFirstLoginToday(Player player) {
		int lastRewardDay = player.getPool().getIntValue(PlayerPool.PROPERTY_LAST_GET_DAILY_REWARD_DAY);
		return lastRewardDay < TimeUpdater.getInstance().today();
	}

}
